package com.ruiyian.project.road.controller;

import com.ruiyian.common.utils.SecurityUtils;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.project.road.domain.Dire;
import com.ruiyian.project.road.domain.DirePoint;
import com.ruiyian.project.road.service.DireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/1/19
 * Desc: 道路控制层
 */
@Slf4j
@RestController
@RequestMapping("/road/dire")
public class DireController extends BaseController {
    @Autowired
    private DireService direService;

    /**
     * 查询列表
     * @param dire
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Dire dire) {
        startPage();
        List<Dire> list = direService.selectList(dire);
        return getDataTable(list);
    }

    /**
     * 根据道路id获取方向
     * @param dire
     * @return
     */
    @PostMapping("/selectListByRoadId")
    public AjaxResult selectListByRoadId(@RequestBody Dire dire) {
        List<Dire> list = null;
        try {
            list = direService.selectListByRoadId(dire);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }
    /**
     * 方向下拉
     * @param dire
     * @return
     */
    @PostMapping("/listAll")
    public AjaxResult listAll(@RequestBody Dire dire) {
        List<Dire> list = null;
        try {
            list = direService.selectList(dire);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(list);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            direService.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody List<Dire> list) {
        try {
            direService.deleteBatch(list);
        } catch (Exception e) {
            logger.error(e.getMessage());
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */

    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Dire dire = null;
        try {
            dire =  direService.findOne(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(dire);
    }

    /**
     * 保存
     * @param road
     * @return
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Dire road) {
        try {
            if(road.getId()!=null){
                road.setUpdateBy(SecurityUtils.getUsername());
                direService.edit(road);
            }else {
                road.setCreateBy(SecurityUtils.getUsername());
                direService.save(road);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(road);
    }


    @PostMapping("/savePoint")
    public AjaxResult listAll(@RequestBody List<DirePoint> direPointList) {
        try {
            for(DirePoint d: direPointList){
                direService.savePoint(d);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据方向id
     * @param id
     * @return
     */
    @PostMapping("/queryByDireId")
    public AjaxResult listAll(@RequestBody Long id) {
        List<DirePoint> direPoints = null;
        try {
           direPoints = direService.queryByDireId(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return AjaxResult.success(direPoints);
    }

    /**
     * 根据道路id查询方向经纬度集合
     * @param id
     * @return
     */
    @PostMapping("/queryByRoadId")
    public AjaxResult queryByRoadId(@RequestBody Long id) {
        Map<Long, List<DirePoint>> collect = null;
        try {
            List<DirePoint> direPoints = direService.queryByRoadId(id);
            collect = direPoints.stream().collect(Collectors.groupingBy(DirePoint::getRoadDireId));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return AjaxResult.success(collect);
    }

}
